package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.util.ObjectUtil;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.system.entity.QuestionAnswer;
import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.guns.modular.system.mapper.QuestionMapper;
import cn.stylefeng.guns.modular.system.service.QuestionService;
import cn.stylefeng.guns.modular.system.service.UserService;
import cn.stylefeng.guns.modular.system.warpper.QuestionWarpper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 评论及回复功能
 */
@Controller
@RequestMapping("/question")
public class QuestionController extends BaseController {

    private static String PREFIX = "/system/question/";

    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserService userService;


    /**
     * 跳转到活动详情页面
     */
    @RequestMapping("/question_add")
    public String questionAdd(){
        return PREFIX + "question_add.html";
    }
    @RequestMapping("/answer_add")
    public String answerAdd(){
        return PREFIX + "answer_add.html";
    }

    /**
     * 跳转到谁问我界面
     */
    @RequestMapping("/myquestion")
    public String question(){
        return PREFIX + "myquestion.html";
    }



    @RequestMapping("/question")
    @ResponseBody
    public ResponseData question(@RequestParam String userId,
                                 @RequestParam String username,
                                 @RequestParam String question){
        //userId  问了谁    username 问了谁   question  问题   user 谁问的


        Long questionId = ShiroKit.getUserNotNull().getId();
        if (ToolUtil.isEmpty(questionId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        User user = userService.getById(questionId);
        QuestionAnswer questionAnswer = new QuestionAnswer();
        questionAnswer.setUserId(Long.valueOf(userId));
        questionAnswer.setUsername(username);
        questionAnswer.setQuestion(question);
        questionAnswer.setQuestionId(questionId);
        questionAnswer.setQuestionName(user.getName());
        //查询questionId之前有没有问过userId其他问题,如果有则更新  没有则插入
        QuestionAnswer byId = questionService.selectByQuestionId(Long.valueOf(userId),questionId );
        if (ToolUtil.isEmpty(byId)) {
            boolean save = questionService.save(questionAnswer);
        }else {
            questionAnswer.setAnswerId(byId.getAnswerId());
            boolean b = questionService.updateById(questionAnswer);
        }
        return SUCCESS_TIP;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list(){

        //查询出所有有关于我的问题


        Long userId = ShiroKit.getUserNotNull().getId();
        if (ToolUtil.isEmpty(userId)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //返回数据   会有多条数据
        List<Map<String, Object>> questionAnswers = questionService.selectByUserId(userId);
        return new QuestionWarpper(questionAnswers).wrap();
    }

    @RequestMapping("/answer")
    @ResponseBody
    public Object answer(@RequestParam String answer, @RequestParam String questionId){
        //questionId   回答了谁的问题  userId  我回答了   answer  我回答了什么


        Long userId = ShiroKit.getUserNotNull().getId();
        if (ToolUtil.isEmpty(userId)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        if (ToolUtil.isEmpty(questionId)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        if (ToolUtil.isEmpty(answer)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }



        Long id = Long.valueOf(questionId);

        //questionAnswer  查询出这条问题的数据

        QuestionAnswer questionAnswer = questionService.selectByQuestionId(userId, id);


        if (ToolUtil.isEmpty(questionAnswer)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //将回答放入这条数据中
        questionAnswer.setAnswer(answer);


        //新建一条信息
        QuestionAnswer now = new QuestionAnswer();

        //更新数据信息为我问了问我问题的人  ，问题变成了回答
        questionAnswer = ObjectUtil.replaceObject(questionAnswer , now);

        //更新数据库信息   变为点对点传输
        questionService.updateById(questionAnswer);

        return SUCCESS_TIP;
    }

    /*@RequestMapping("/info")
    @ResponseBody
    public Object msg(){
        Long userId = ShiroKit.getUserNotNull().getId();
        if (ToolUtil.isEmpty(userId)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        QuestionAnswer questionAnswer = questionService.getById(userId);
        if (ToolUtil.isEmpty(questionAnswer)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        return ResponseData.success(questionAnswer);
    }*/


}
